GBML manual / Arrays

array_filter

Makes a new LIST of just the values of target that the script test says yes to, in their original order. test is the NAME of one of your scripts; it takes one argument and gives back true or false. The answer is a list, not an array, because how many pass is only known while the game runs. target is not changed.

Syntax

array_filter(target, test) -> list<T>

Arguments

NameType
targetarray
testscript

Returns

list<T>

Description

Makes a new list holding only the values of target, an array, that a script says yes to. The script is asked about each value in turn. It answers true to keep the value and false to leave it out. The values that are kept stay in the order they had. target itself is not changed.

var high = array_filter(scores, is_high);

If scores holds 30, 80, 55 and 10 and is_high says yes to 50 or more, high holds 80 and 55.

The script

The second thing you give is the name of one of your scripts: a file in the project's scripts folder with a function named like the file. Write just the name, with no brackets after it. Here is a script that answers whether a score is high:

// scripts/is_high.gbml
function is_high(score: int): bool {
    return score >= 50;
}

The script has to follow four rules, and each one is checked before the game runs:

A function written out in place

You do not have to make a script file for a short job. Write the function right where the name would go:

var high = array_filter(nums, function (n) { return n >= 50; });

It is the script, only without a name. Its one parameter takes the kind of the values of the array, so you do not write the kind (you may, and then it has to fit). It can read and change the variables around it, such as a local you made on the line before, and a return inside it leaves only the function, never the event or script around it. Use a script file instead when the same job is needed in more than one place.

It gives back a list, not an array

The answer is a list, not an array. How many values pass is only found out while the game runs, and an array's size is fixed when the game is built. A list is the container made for exactly this: it has a fixed capacity and a length that goes up and down. So you ask the answer how many passed with list_length, and you can go on to add to it or take from it like any list.

How big the new list is

The new list has room for as many values as the array has places, because the most that could pass is every value. An array of 40 places makes a list with room for 40, even if only 3 values pass. So a filter costs a whole list's worth of memory. If the array is a variable of an object, every instance of the object carries its own.

For the same reason a filter can only be written in one place: the whole right-hand side of the line that makes the variable, like var high = array_filter(scores, is_high);, or high = array_filter(scores, is_high); in an object's Create event. It cannot go inside another call or a sum, on a line by itself, over a variable that already exists, or in a later event such as Step.

It also needs to know how big the array you filter is, so that has to be a plain variable name whose size is known. A script's parameter could be any size, so it is refused. A filter cannot be given a size of its own either.

What happens in the awkward cases

You do thisWhat happens
the script says no to every valuethe new list is empty
the script says yes to every valuethe new list holds all of them, in the same order
an array of text, structs or referencesworks. Nothing is compared, the script decides
give a script that is not a script's name, has the wrong number of parameters, the wrong kind of parameter, or does not answer true or falserefused when checked, and the message says which
give it a list or a gridrefused when checked, and the message names the function for that kind, like list_filter
use it where a value is needed, other than as the whole right-hand side of the line that makes a variablerefused when checked

What it costs

One call of your script for each place the array has, and a whole list of memory with room for as many values as the array has places. The array is not changed.

Example

var scores = array_create(4, 0);
scores[0] = 30;
scores[1] = 80;
scores[2] = 55;
scores[3] = 10;
var high = array_filter(scores, is_high);   // a list holding 80, 55
var how_many = list_length(high);           // 2

On the GBA

A fixed-size vector with the same N as the array, filled by a loop that adds each value the script accepts. The answer is a LIST, not an array: how many items pass is only found out while the game runs, and an array's size is fixed when the game is built, whereas a list has a fixed capacity and a length that changes. Its capacity is the original's (the most that could pass is all of them), so it is a second whole container of memory: as many places as the original has room for, not just how many pass. Like a copy, it has to be the whole right-hand side of the line that makes the variable, the original has to be a variable whose size is known (a script's parameter could be any size), and it cannot be given a size of its own. The script is called once for each item, in order, and the items it says yes to are added to the answer in the same order; the original is not changed. The script has to give back true or false, take exactly one argument whose type accepts the items, and be the NAME of one of your scripts: a function written out in the middle of the line is not turned into game code yet, so it is refused instead of doing nothing. O(n) calls of the script, and no timing was measured.

See also: list_filtergrid_filterarray_foreacharray_copyarray_lengtharray_createlist_lengthArraysScriptsWriting a scriptData containers